import { ref } from "vue";
import { messageError } from "@/utils/message";

export function useApiLock(callback, msgText = "") {
    if (!(callback && typeof callback == "function")) return;
    const lock = ref(false);

    const apiCall = async (params) => {
        if (lock.value) {
            if (msgText) messageError(msgText ? msgText : "请勿重复请求");
            return false;
        }
        lock.value = true;

        const result = await callback(params).catch((e) => {
            lock.value = false;
            return Promise.reject(e);
        });

        lock.value = false;
        return result;
    };

    return {
        lock,
        apiCall,
    };
}
